using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Super
{
	public class FWSetExtension : EditorWindow
	{
		public int optionIndex = 0;
		public int cntIndex = 0;

		public string addKey = "";
		public string RemoveKey = "";
		public string optionKey = "";
		public string optionName = "";

		public bool editable = false;
		public FWSetShowType oldShowType = FWSetShowType.Switch;
		public FWSetShowType showType = FWSetShowType.Switch;


		public Vector2 index_ScrollPos = Vector2.zero;
		public Vector2 content_ScrollPos = Vector2.zero;

		public string[] showTypeStrings = new string[] { "开关", "路径", "数值" };

		[MenuItem("Framework/FWSetWindow")]
		public static void ShowWindow()
		{
			FWSetExtension window = GetWindow<FWSetExtension>();

			window.Show();
		}

		private void OnGUI()
		{
			GUILayout.BeginVertical();

			RefreshTitle();     //刷新title
			GUILayout.BeginHorizontal();
			RefreshOptions();           //刷新选项
			GUILayout.Box("", GUILayout.Width(5), GUILayout.ExpandHeight(true));    //元素间隔
			if (FWSetManager.GetOptionNum() <= optionIndex)
			{
				optionIndex = 0;
			}
			else
			{
				RefreshContent();			//刷新选项内容
			}
			GUILayout.EndHorizontal();

			GUILayout.EndVertical();
		}

		public void RefreshTitle()
		{
			GUILayout.BeginHorizontal();
			GUILayout.Label("选项", EditorStyles.boldLabel, GUILayout.Width(150));
			GUILayout.Label("显示模式：", EditorStyles.boldLabel);
			showType = (FWSetShowType)EditorGUILayout.Popup((int)showType, showTypeStrings, GUILayout.Width(50));
			if (showType != oldShowType)
			{
				FWSetManager.Ins.optionNames[optionIndex].showType = showType;
			}
			oldShowType = showType;
			GUILayout.FlexibleSpace();

			editable = GUILayout.Toggle(editable, "编辑");
			GUILayout.Space(10);
			GUI.enabled = editable;
			if (GUILayout.Button("删除配置"))
			{
				FWSetManager.RemoveOption(optionIndex);
				optionIndex = 0;
			}
			GUILayout.Label("key:", EditorStyles.boldLabel);
			optionKey = GUILayout.TextField(optionKey, GUIStyleUtil.GetTextAreaCenter(15), GUILayout.Width(100));
			GUILayout.Label("名称:", EditorStyles.boldLabel);
			optionName = GUILayout.TextField(optionName, GUIStyleUtil.GetTextAreaCenter(15), GUILayout.Width(100));
			if (GUILayout.Button("增加配置"))
			{
				FWSetManager.AddOption(optionKey, optionName, showType);
			}

			if (GUILayout.Button("保存"))
			{
				FWSetManager.SaveData();
				LogUtil.Log(LogType.Normal, LogCategory.Default, "保存配置修改成功");
				//EditorUtil.RecompileScript();
			}
			GUI.enabled = true;
			GUILayout.Space(20);
			GUILayout.EndHorizontal();
		}
		public void RefreshOptions()
		{
			index_ScrollPos = GUILayout.BeginScrollView(index_ScrollPos, GUILayout.Width(125));
			GUILayout.BeginVertical(GUILayout.Width(120));
			List<FWOptionNode> options = FWSetManager.Ins.optionNames;
			for (int i = 0; i < options.Count; i++)
			{
				string name = options[i].name + "\n" + options[i].key;
				if (GUILayout.Button(name, GUIStyleUtil.GetButtonSelectGUIStyle(optionIndex == i), GUILayout.Height(50)))
				{
					optionIndex = i;
					showType = options[i].showType;
					oldShowType = showType;
				}
			}
			GUILayout.EndVertical();
			GUILayout.EndScrollView();
		}

		public void RefreshContent()
		{
			content_ScrollPos = GUILayout.BeginScrollView(content_ScrollPos, GUILayout.ExpandWidth(true));
			Dictionary<string, FWSettingNode> dict = FWSetManager.GetOption(optionIndex);
			RefreshContentTop(dict);
			switch (showType)
			{
				case FWSetShowType.Switch:
					RefreshToggle(dict);
					break;
				case FWSetShowType.Path:
					RefreshPath(dict);
					break;
				case FWSetShowType.Num:
					RefreshNum(dict);
					break;
			}
			GUILayout.EndVertical();
			GUILayout.EndScrollView();
		}
		public void RefreshContentTop(Dictionary<string, FWSettingNode> dict)
		{
			GUILayout.BeginVertical();
			GUILayout.BeginHorizontal(new GUIStyle(GUI.skin.button));
			RemoveKey = GUILayout.TextField(RemoveKey);
			if (GUILayout.Button(GUIStyleUtil.GetAddOrRomove(false), GUILayout.Width(50), GUILayout.Height(50)))
			{
				dict.Remove(RemoveKey);
			}

			addKey = GUILayout.TextField(addKey);
			if (GUILayout.Button(GUIStyleUtil.GetAddOrRomove(true), GUILayout.Width(50), GUILayout.Height(50)))
			{
				dict.Remove(addKey);
				dict[addKey] = new FWSettingNode(addKey);
			}
			GUILayout.Space(15);
			GUILayout.EndHorizontal();
		}

		public void RefreshToggle(Dictionary<string, FWSettingNode> dict)
		{
			foreach (var item in dict)
			{
				GUILayout.BeginHorizontal(new GUIStyle(GUI.skin.button));

				GUIStyle style = GUIStyleUtil.GetTextCenter(13);
				style.wordWrap = true;
				style.fontStyle = FontStyle.Bold;
				GUILayout.Label(item.Key, style, GUILayout.Width(170), GUILayout.Height(50));
				if (GUILayout.Button("复制", GUILayout.Width(50), GUILayout.Height(50)))
				{
					GUIUtility.systemCopyBuffer = item.Key;
					addKey = item.Key;
					RemoveKey = item.Key;
				}
				if (GUILayout.Button(GUIStyleUtil.GetButtonCheckTexture(item.Value.isActive), GUILayout.Width(50), GUILayout.Height(50)))
				{
					bool isActive = item.Value.isActive;
					dict[item.Key].isActive = !isActive;
				}
				string note = GUILayout.TextArea(item.Value.note, GUIStyleUtil.GetTextAreaCenter(13), GUILayout.MinWidth(250), GUILayout.Height(50));
				if (note != item.Value.note)
				{
					dict[item.Key].note = note;
				}
				GUILayout.EndHorizontal();
			}
		}
		public void RefreshPath(Dictionary<string, FWSettingNode> dict)
		{
			foreach (var item in dict)
			{
				var value = dict[item.Key];
				GUILayout.BeginHorizontal(new GUIStyle(GUI.skin.button));

				GUIStyle style = GUIStyleUtil.GetTextCenter(13);
				style.wordWrap = true;
				style.fontStyle = FontStyle.Bold;
				GUILayout.Label(value.key, style, GUILayout.Width(170), GUILayout.Height(50));
				if (GUILayout.Button("复制", GUILayout.Width(50), GUILayout.Height(50)))
				{
					GUIUtility.systemCopyBuffer = value.key;
					addKey = value.key;
					RemoveKey = value.key;
				}

				string path = GUILayout.TextArea(value.str, GUIStyleUtil.GetTextAreaCenter(13), GUILayout.MinWidth(250), GUILayout.MinHeight(50));
				if (path != value.str)
				{
					value.str = path;
				}
				var showNotes = GUIStyleUtil.FoldoutCenter(value.showNotes, "备注");
				value.showNotes = showNotes;
				GUILayout.EndHorizontal();

				if (showNotes)
				{
					value.showNotes = showNotes;
					var note = GUILayout.TextArea(value.note, GUIStyleUtil.GetTextAreaCenter(13), GUILayout.MinWidth(250), GUILayout.MinHeight(50));
					if (note != value.note)
					{
						value.note = note;
					}
				}
			}
		}
		public void RefreshNum(Dictionary<string, FWSettingNode> dict)
		{
			foreach (var item in dict)
			{
				var value = dict[item.Key];
				GUILayout.BeginHorizontal(new GUIStyle(GUI.skin.button));

				GUIStyle style = GUIStyleUtil.GetTextCenter(13);
				style.wordWrap = true;
				style.fontStyle = FontStyle.Bold;
				GUILayout.Label(value.key, style, GUILayout.Width(170), GUILayout.Height(50));
				if (GUILayout.Button("复制", GUILayout.Width(50), GUILayout.Height(50)))
				{
					GUIUtility.systemCopyBuffer = value.key;
					addKey = value.key;
					RemoveKey = value.key;
				}

				string numStr = GUILayout.TextArea(value.numInt.ToString(), GUIStyleUtil.GetTextAreaCenter(13), GUILayout.MinWidth(250), GUILayout.MinHeight(50));
				if (numStr != value.numInt.ToString())
				{
					value.numInt = int.Parse(numStr);
				}
				var showNotes = GUIStyleUtil.FoldoutCenter(value.showNotes, "备注");
				value.showNotes = showNotes;
				GUILayout.EndHorizontal();

				if (showNotes)
				{
					value.showNotes = showNotes;
					var note = GUILayout.TextArea(value.note, GUIStyleUtil.GetTextAreaCenter(13), GUILayout.MinWidth(250), GUILayout.MinHeight(50));
					if (note != value.note)
					{
						value.note = note;
					}
				}
			}
		}
	}

}
